package alibaba.server;

import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;

public class Listing {

	private Map<String,ListingEntry> listOfFiles;   
	
	public Listing(Map<String, ListingEntry> listOfFiles) {
		super();
		this.listOfFiles = listOfFiles;
	}

	public Listing() {
		this.listOfFiles = new HashMap<String, ListingEntry>();
	}

	public Map<String, ListingEntry> getListOfFiles() {
		return listOfFiles;
	}

	public void setListOfFiles(Map<String, ListingEntry> listOfFiles) {
		this.listOfFiles = listOfFiles;
	}

	/**
	 * Write the backup server meta information repository to disk
	 * @param filename
	 * @param listing
	 * @throws FileNotFoundException 
	 */
	public static void writeListing(String filename, Listing listing) throws FileNotFoundException {
		XMLEncoder xmlEncoder = new XMLEncoder(new BufferedOutputStream(new FileOutputStream(filename)));
		xmlEncoder.writeObject(listing);
		xmlEncoder.close();
	}
	
	/**
	 * Read the backup server meta information repository from disk
	 * @param filename
	 * @return
	 * @throws FileNotFoundException 
	 */
	public static Listing readListing(String filename) throws FileNotFoundException {
		Listing result = null;
		XMLDecoder xmlDecoder = new XMLDecoder(new BufferedInputStream(new FileInputStream(filename)));
		result = (Listing) xmlDecoder.readObject();
		xmlDecoder.close();
		return result;
	}
	
}
